package com.sca.product.controller;

import com.sca.product.entity.CommonResult;
import com.sca.product.entity.Product;
import com.sca.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @PostMapping(value = "/create")
    public CommonResult<Integer> create(@RequestBody Product product) throws Exception {
        try {
            int result = productService.create(product);
            log.info("****插入结果：" + result);
            if (result > 0) {
                return new CommonResult(200, "插入数据库成功", result);
            } else {
                return new CommonResult(444, "插入数据库失败", null);
            }
        } catch (Exception e) {
            log.info(e.toString());
            throw e;
            //return new CommonResult(555, "插入数据库失败", null);
        }
    }

    @GetMapping("/get/{id}")
    public CommonResult<Product> getPaymentById(@PathVariable("id") Long id ){
        Product payment = productService.getProductById(id);
        log.info("****查询结果："+payment);
        if (payment != null){
            return new CommonResult(200,"查询成功",payment);
        }else {
            return new CommonResult(444,"没有对应记录，查询ID："+id,null);
        }
    }

}
